<?php
require('../../../wp-config.php');
$plugin_url = trailingslashit( WP_PLUGIN_URL.'/'. dirname( plugin_basename(__FILE__)));
global $wpdb;
$row = $wpdb->get_row("SELECT * FROM wp_house WHERE id=".intval($_GET['id']));
?>
<script>
    jQuery('#upload_image_button').click(function() {
 formfield = jQuery('#upload_image').attr('name');
 tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
 return false;
});

window.send_to_editor = function(html) {
 imgurl = jQuery('img',html).attr('src');
 lasthtml = jQuery("#display_image").html();
  jQuery("#display_image").html(lasthtml+'&nbsp;<img src="'+imgurl+'" /><img src="<?php echo $plugin_url;?>images/delete.png" onclick="remove_image(\''+imgurl+'\');" style="cursor:pointer;" /><a href="javascript:void(0);" onclick="add_main(\''+imgurl+'\');" >set main picture</a>');
  if(jQuery("#image").val() == ''){
      jQuery("#image").val(imgurl);
  }
  else
      {
          jQuery("#image").val(jQuery("#image").val()+','+imgurl);
      }
 tb_remove();
}
function remove_image(value){
    if(confirm("Are you sure you want to remove this image ? ")){
   var string = jQuery("#image").val();
   string = string.replace(value+',','');
   string = string.replace(','+value,'');
   string = string.replace(value,'');
   jQuery("#image").val(string);
   array = string.split(',');
   value = '';
   for (i in array){
       value = value +'&nbsp;<img src="'+array[i]+'" /><img src="<?php echo $plugin_url;?>images/delete.png" onclick="remove_image(\''+array[i]+'\');" style="cursor:pointer;" /><a href="javascript:void(0);" onclick="add_main(\''+array[i]+'\');" >set main picture</a>';
   }
   jQuery("#display_image").html(value);
   }
}
function add_main(value){
    if(confirm("Add this picture as main image ?")){
        jQuery("#main_image").val(value);
        string = jQuery("#image").val();
       array = string.split(',');
       value2 = '';
       for (i in array){
           if(array[i] != value){
               value2 = value2 +'&nbsp;<img src="'+array[i]+'" /><img src="<?php echo $plugin_url;?>images/delete.png" onclick="remove_image(\''+array[i]+'\');" style="cursor:pointer;" /><a href="javascript:void(0);" onclick="add_main(\''+array[i]+'\');" >set main picture</a>';
           }
           else
           {
               value2 = value2 +'&nbsp;<img src="'+array[i]+'" /><img src="<?php echo $plugin_url;?>images/delete.png" onclick="remove_image(\''+array[i]+'\');" style="cursor:pointer;" />';
           }
           }
       jQuery("#display_image").html(value2);
    }
}
</script>
<div id="loading" align="center" style="display:none;"><img src="<?php echo $plugin_url;?>images/loading.gif" /><br />Loading...</div>
<strong>Edit listing</strong>
<p></p>
<form method="post" action="<?php echo $plugin_url; ?>edit_house.php?id=<?php echo intval($_GET['id']);?>" name="edit_house"  enctype="multipart/form-data">
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td>
            Code :
        </td>
        <td>
            <input type="text" name="code" size="60" value="<?php echo $row->code;?>" />
        </td>
        </tr>
        <tr>
        <td>
            Name :
        </td>
        <td>
            <input type="text" name="name"  size="60" value="<?php echo $row->name;?>" />
        </td>
    </tr>
           <tr>
        <td>
            Address :
        </td>
        <td>
            <input type="text" name="address"  size="60" value="<?php echo $row->address;?>" />
        </td>
    </tr>
      <tr>
        <td>
            Bedrooms :
        </td>
        <td>
            <select name="bed_room">
                <option value="1" <?php if(intval($row->bed_room) == 1){echo 'selected';} ?>>1</option>
                <option value="2" <?php if(intval($row->bed_room) == 2){echo 'selected';} ?>>2</option>
                <option value="3" <?php if(intval($row->bed_room) == 3){echo 'selected';} ?>>3</option>
                <option value="4" <?php if(intval($row->bed_room) == 4){echo 'selected';} ?>>4 or more</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>
            Property Type :
        </td>
        <td>
            <select name="type">
	       <option value="2" <?php if(intval($row->type) == 2){echo 'selected';} ?>>Single Family</option>
               <option value="3" <?php if(intval($row->type) == 3){echo 'selected';} ?>>Cottage</option>
               <option value="4" <?php if(intval($row->type) == 4){echo 'selected';} ?>>Duplex or Condo</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>
            Location :
        </td>
        <td>
            <select name="location">
		<option value="2" <?php if(intval($row->location) == 2){echo 'selected';} ?>>Barnstable</option>
                <option value="3" <?php if(intval($row->location) == 3){echo 'selected';} ?>>Boston</option>
                <option value="4" <?php if(intval($row->location) == 4){echo 'selected';} ?>>Bourne</option>
                <option value="5" <?php if(intval($row->location) == 5){echo 'selected';} ?>>Brewster</option>
                <option value="6" <?php if(intval($row->location) == 6){echo 'selected';} ?>>Cape Cod</option>
                <option value="7" <?php if(intval($row->location) == 7){echo 'selected';} ?>>Centerville</option>
                <option value="8" <?php if(intval($row->location) == 8){echo 'selected';} ?>>Chatham</option>
                <option value="9" <?php if(intval($row->location) == 9){echo 'selected';} ?>>Cohasset</option>
                <option value="10" <?php if(intval($row->location) == 10){echo 'selected';} ?>>Cotuit</option>
                <option value="11" <?php if(intval($row->location) == 11){echo 'selected';} ?>>Craigville</option>
                <option value="12" <?php if(intval($row->location) == 12){echo 'selected';} ?>>Cummaquid</option>
                <option value="13" <?php if(intval($row->location) == 13){echo 'selected';} ?>>Dennis</option>
                <option value="14" <?php if(intval($row->location) == 14){echo 'selected';} ?>>Falmouth</option>
                <option value="15" <?php if(intval($row->location) == 15){echo 'selected';} ?>>Great Island</option>
                <option value="16" <?php if(intval($row->location) == 16){echo 'selected';} ?>>Harwich</option>
                <option value="17" <?php if(intval($row->location) == 17){echo 'selected';} ?>>Hyannis</option>
                <option value="18" <?php if(intval($row->location) == 18){echo 'selected';} ?>>Hyannisport</option>
                <option value="19" <?php if(intval($row->location) == 19){echo 'selected';} ?>>Marion</option>
                <option value="20" <?php if(intval($row->location) == 20){echo 'selected';} ?>>Marstons Mills</option>
                <option value="21" <?php if(intval($row->location) == 21){echo 'selected';} ?>>Mashpee</option>
                <option value="22" <?php if(intval($row->location) == 22){echo 'selected';} ?>>Nantucket</option>
                <option value="23" <?php if(intval($row->location) == 23){echo 'selected';} ?>>New Seabury</option>
                <option value="24" <?php if(intval($row->location) == 24){echo 'selected';} ?>>Orleans</option>
                <option value="25" <?php if(intval($row->location) == 25){echo 'selected';} ?>>Osterville</option>
                <option value="26" <?php if(intval($row->location) == 26){echo 'selected';} ?>>Other</option>
                <option value="27" <?php if(intval($row->location) == 27){echo 'selected';} ?>>Pocasset</option>
                <option value="28" <?php if(intval($row->location) == 28){echo 'selected';} ?>>Popponessett</option>
                <option value="29" <?php if(intval($row->location) == 29){echo 'selected';} ?>>Sandwich</option>
                <option value="30" <?php if(intval($row->location) == 30){echo 'selected';} ?>>Willowbend</option>
                <option value="31" <?php if(intval($row->location) == 31){echo 'selected';} ?>>Woods Hole</option>
                <option value="32" <?php if(intval($row->location) == 32){echo 'selected';} ?>>Yarmouth</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>
            Price :
        </td>
        <td>
            <input type="text" name="price"  size="60" value="<?php echo $row->price;?>" />
        </td>
    </tr>
    <tr>
        <td>
            Description :
        </td>
        <td>
            <textarea name="description" id="description" cols="57" rows="15"><?php echo $row->description;?></textarea>
        </td>
    </tr>
    <tr valign="top">
<td >Image</td>
<td><label for="upload_image">
<input id="upload_image_button" type="button" value="Add Image" />
<div id="display_image">
    <?php 
    foreach($wpdb->get_results("SELECT * FROM wp_image WHERE id_house=".$_GET['id']) as $image):
            $array[] = $image->link;
    ?>
    &nbsp;<img src="<?php echo $image->link;?>" /><img src="<?php echo $plugin_url;?>images/delete.png" onclick="remove_image('<?php echo $image->link;?>');" style="cursor:pointer;" /><?php if($image->link != $wpdb->get_var("SELECT link FROM wp_image WHERE id_house=".$_GET['id']." AND main=1")){?><a href="javascript:void(0);" onclick="add_main('<?php echo $image->link;?>');" >set as main picture</a><?}?>
    <?php endforeach;?>
</div>
<input type="hidden" name="image" id="image" value="<?php if(count($array) > 0){echo implode(',',$array);}?>" />
<input type="hidden" name="main_image" id="main_image" value="<?php echo $wpdb->get_var("SELECT link FROM wp_image WHERE id_house=".$_GET['id']." AND main=1"); ?>" />
</label></td>
</tr>
</table>
    <?php foreach($wpdb->get_results("SELECT * FROM wp_category") as $category):?>
<div style="background-color:#CCCCCC;color:white;font-weight: bold;margin-top:10px;"><?php echo $category->name;?></div>
<br />
<?php foreach($wpdb->get_results("SELECT * FROM wp_attribute WHERE category=".$category->id) as $ro){
    $check = $wpdb->get_var("SELECT COUNT(*) FROM wp_value WHERE id_att=".$ro->id." AND id_house = ".$row->id);
    if($check > 0){
        $check2 = 'checked';
    }
    else
    {
        $check2 = '';
    }
    echo '<input type="checkbox" name="attribute'.$category->id.'['.$ro->id.']" value="1" '.$check2.' />&nbsp;'.$ro->name.'&nbsp;';
    }?>
<?php endforeach;?>
</form>
<p></p>
<a class="button add-new-h2" href="javascript:void(0);" onclick="document.edit_house.submit();">Edit</a>